package com.itlong.cloud.service.sdkv3.impl;

import com.itlong.cloud.POJO.PO.PropertyUserInfoPO;
import com.itlong.cloud.POJO.VO.SDKMessageV3;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.SDKV3ErrorCodeEnum;
import com.itlong.cloud.service.sdkv3.ICheckService;
import com.itlong.cloud.user.property.IPropertyUserServiceClient;
import com.itlong.cloud.utils.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class CheckServiceImpl implements ICheckService {

    @Autowired
    IPropertyUserServiceClient iPropertyUserServiceClient;

    @Override
    public SDKMessageV3 check1(String requestId, String appId, String roleId) {
        //appId被删除或者禁用
        PropertyUserInfoPO propertyUserInfoPO = iPropertyUserServiceClient.getByUserId(appId);
        if (propertyUserInfoPO == null) {
            return new SDKMessageV3(SDKV3ErrorCodeEnum.APP_ID_STATUS_ERROR.getErrorCode(), requestId);
        }
        //对接方式(需要包含云对接->1)
        if (propertyUserInfoPO.getDockMode() == null || !propertyUserInfoPO.getDockMode().contains("1")) {
            return new SDKMessageV3(SDKV3ErrorCodeEnum.DOCK_MODE_ERROR.getErrorCode(), requestId);
        }
        //机器人开发者账号roleId=7
        if (roleId == null || !roleId.equals("7") || propertyUserInfoPO.getRoleId() == null || !propertyUserInfoPO.getRoleId().equals("7")) {
            return new SDKMessageV3(SDKV3ErrorCodeEnum.DEVELOP_TYPE_ERROR.getErrorCode(), requestId);
        }
        return new SDKMessageV3(BaseErrorCodeEnum.SUCCESS.getErrorCode(), requestId);
    }

    @Override
    public SDKMessageV3 check2(String requestId, long requestTime, Map<String, Object> AuthStatus) {
        //appId被删除或者禁用
        if (AuthStatus == null) {
            return new SDKMessageV3(SDKV3ErrorCodeEnum.APP_ID_STATUS_ERROR.getErrorCode(), requestId);
        }
        if (AuthStatus.get("roleId") == null || !AuthStatus.get("roleId").toString().equals("7")) {
            return new SDKMessageV3(SDKV3ErrorCodeEnum.DEVELOP_TYPE_ERROR.getErrorCode(), requestId);
        }
        //自定义授权时间范围
        long startTime = DateUtil.dateToTimestamp(AuthStatus.get("startTime").toString());
        long endTime = DateUtil.dateToTimestamp(AuthStatus.get("endTime").toString());
        if (AuthStatus.get("authValidityPeriodSettype").toString().equals("0") && (requestTime < startTime || requestTime > endTime)) {
            return new SDKMessageV3(SDKV3ErrorCodeEnum.AUTH_TIME_ERROR.getErrorCode(), requestId);
        }
        return new SDKMessageV3(BaseErrorCodeEnum.SUCCESS.getErrorCode(), requestId);
    }

}